import wepy from 'wepy'
export default class extends wepy.mixin {
	data = {
		radio: 0,
		// 会员id
		memberId: '',
		pageIndex: '1',
		pageSize: '11',
		// 收货地址信息
		getaddressInfo: [],
		stated:''
	}
	onLoad(options) {
		if(options.stated){
			this.stated=options.stated;
		}
		console.log('个人信息页面已经加载')
		let uesrId = wepy.getStorageSync('userId')
		this.memberId = uesrId
		this.getAddressList()
	}
	onShow() {}
	methods = {
		async onChange(e) {
			// this.radio = parseInt(e.detail)
			console.log(e.target.dataset.id)
			// 设置默认
			const res = await wepy.post('baseRest/manageAddr', {
				flag: 'default',
				memberId: this.memberId,
				addrId: e.target.dataset.id,
			})
			console.log(res)
			if (res.statusCode != 200) {
				return wepy.noToast('修改默认地址失败')
			}
			this.getAddressList()
		},
		//修改收货地址
		edit(id) {
			console.log(id)
			wepy.redirectTo({
				url: '../../pages/editAddress/editaddress?did=' + id,
			})
		},
		//支付时选择收货地址
		async zhiaddress(id){
			if(this.stated==1){
				return
			}else{
				const res = await wepy.post('baseRest/manageAddr', {
					flag: 'default',
					memberId: this.memberId,
					addrId: id,
				})
				console.log(res)
				if (res.statusCode != 200) {
					return wepy.noToast('修改默认地址失败')
				}else{
					wx.navigateBack({
					   delta: 1,
					})
				}
			}
		},
		removesd(id) {
			console.log(id)
			if (this.getaddressInfo.length <= 1) {
				return wepy.noToast('至少保留一个地址')
			}
			var that = this
			wx.showModal({
				title: '删除地址',
				content: '是否删除该收货地址？',
				success(res) {
					if (res.confirm) {
						that.removep(id)
						console.log('点击确定')
					} else if (res.cancel) {
						console.log('用户点击取消')
					}
				},
			})
		},
		//新增收货地址
		editAddress() {
			wepy.redirectTo({
				url: '../../pages/editAddress/editaddress',
			})
		},
	}
	//   获取收货地址
	async getAddressList() {
		const res = await wepy.post('baseRest/getShAddrList', {
			memberId: this.memberId,
			pageIndex: this.pageIndex,
			pageSize: this.pageSize,
		})
		console.log(res)
		if (res.statusCode != 200) {
			return wepy.noToast('获取地址信息失败')
		}
		this.getaddressInfo = res.data.data
		this.$apply()
	}
	// 删除地址
	async removep(id) {
		const res = await wepy.post('baseRest/manageAddr', {
			flag: 'delete',
			memberId: this.memberId,
			addrId: id,
		})
		console.log(res, '------------')
		if (res.data.state == 1) {
			var that=this;
			wepy.baseToast('删除成功')
			setTimeout(function() {
				that.getAddressList()
			}, 2000);
		}
	}
}
